package sz.szsmk.citizencard.mvp.presenter;

import android.widget.Toast;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.CheckThirdAppReq;
import sz.szsmk.citizencard.model.network.bean.request.CheckUserRequest;
import sz.szsmk.citizencard.model.network.bean.request.GetCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.ImgCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.ProtocolsRequest;
import sz.szsmk.citizencard.model.network.bean.request.ThirdAppLoginReq;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.CheckThirdAppRes;
import sz.szsmk.citizencard.model.network.bean.response.CheckUserByMobileRes;
import sz.szsmk.citizencard.model.network.bean.response.ImgCodeRes;
import sz.szsmk.citizencard.model.network.bean.response.ProtocolsRes;
import sz.szsmk.citizencard.model.network.bean.response.ThirdAppLoginRes;
import sz.szsmk.citizencard.mvp.contract.IGetCodeContact;
import sz.szsmk.citizencard.mvp.contract.IMainContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/5/31 14:52
 * description:获取验证码
 */
public class GetcodePresenter extends IBasePresenter
        implements IGetCodeContact.IGetCodePresenter<IGetCodeContact.IGetCodeView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public GetcodePresenter(GsdxNetApi gsdxNetApi) {

        this.gsdxNetApi = gsdxNetApi;
        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    public void sendGetcodeRequest(String imgCode,String androidId, String type, String usePhone) {

        GetCodeRequest getCodeRequest = new GetCodeRequest();
        getCodeRequest.setImgCode(imgCode);
        getCodeRequest.setDevice_id(androidId);
        getCodeRequest.setChannel_id("000001");
        getCodeRequest.setType_id(type);
        getCodeRequest.setMobile(usePhone);
        String json = new Gson().toJson(getCodeRequest);
        L.e(Constant.TAG, "getCode:" + json);

        mNetworkDateSource.getSms(getCodeRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onFailure(BaseBean response) {
                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }

            @Override
            protected void onOk(BaseBean response) {
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
                ((IGetCodeContact.IGetCodeView) mView).updataSuccess();
            }

            @Override
            protected void onNext(BaseBean response) {
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
                ((IGetCodeContact.IGetCodeView) mView).getCodeFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                Toast.makeText(DaggerApplication.getIns(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });


    }

    @Override
    public void checkUserByMobile(final String type, final String androidId, final String codeType, final String userPhone) {
        CheckUserRequest checkUserRequest = new CheckUserRequest();
        checkUserRequest.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        checkUserRequest.setMobile(userPhone);
        mNetworkDateSource.checkUserByMobile(checkUserRequest, new SubscribeResult<CheckUserByMobileRes>() {
            @Override
            protected void onOk(CheckUserByMobileRes response) {
                int result = response.getResult();
                if (codeType.equals("2")) {
                 //   sendGetcodeRequest(androidId, codeType, userPhone);
                    if (result == 1) {
                        getImgCode(userPhone);
                     //   sendGetcodeRequest(androidId, codeType, userPhone);
                    } else {
                        PubUtils.popTipOrWarn(DaggerApplication.getIns(), "用户不存在");
                        return;
                    }
                }
                if (codeType.equals("1")) {
                    if (result == 1) {

//                        PubUtils.popTipOrWarn(DaggerApplication.getIns(), "用户已存在");
                        ((IGetCodeContact.IGetCodeView) mView).userIshave();
                        return;
                    } else {
                        getImgCode(userPhone);
                        //sendGetcodeRequest(androidId, codeType, userPhone);
                    }
//                        ((IGetCodeContact.IGetCodeView) mView).checkUserSucc(response.getMessage());
//                    }
                }

            }

            @Override
            protected void onNext(CheckUserByMobileRes response) {
                super.onNext(response);
                //用户不存在
//                ((IGetCodeContact.IGetCodeView) mView).checkUserFail(response);
                if (response.getCode().equals("000001")) {
                    getImgCode(userPhone);
                   // sendGetcodeRequest(androidId, codeType, userPhone);
                } else {
                    ((IGetCodeContact.IGetCodeView) mView).checkUserFail(response);
                }
            }

            @Override
            protected void onFailure(CheckUserByMobileRes response) {
                super.onFailure(response);
                if (response.getMessage().contains("用户不存在")) {
//                    getImgCode(userPhone);
                   // sendGetcodeRequest(androidId, codeType, userPhone);
                    PubUtils.popTipOrWarn(DaggerApplication.getIns(), response.getMessage());
                }
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
//                PubUtils.popTipOrWarn(DaggerApplication.getIns(), throwable.getMessage());

            }
        });
    }

    @Override
    public void thirdAppLogin(int login_type, String mobile, String setPsdEtSure, String uid) {
        ThirdAppLoginReq thirdAppLoginReq = new ThirdAppLoginReq();
        thirdAppLoginReq.setDevice_id(setPsdEtSure);
        thirdAppLoginReq.setLogin_type(login_type);
        thirdAppLoginReq.setMobile(mobile);
        thirdAppLoginReq.setPassword("asdfasdf");
        thirdAppLoginReq.setUid(uid);
        String json = new Gson().toJson(thirdAppLoginReq);
        L.e(Constant.TAG, "thirdAppLogin---->" + json);
        mNetworkDateSource.thirdAppLogin(thirdAppLoginReq, new SubscribeResult<ThirdAppLoginRes>() {
            @Override
            protected void onOk(ThirdAppLoginRes response) {
                String access_token = response.getAccess_token();
                String refresh_token = response.getRefresh_token();
                ACache.get(DaggerApplication.getIns()).put("access_token", access_token);
                ACache.get(DaggerApplication.getIns()).put("refresh_token", refresh_token);
                ((IGetCodeContact.IGetCodeView) mView).thirdAppLoginSucc();
            }

            @Override
            protected void onFailure(ThirdAppLoginRes response) {
                super.onFailure(response);
                ((IGetCodeContact.IGetCodeView) mView).thirdAppLoginFail(response.getMessage());
            }

            @Override
            protected void onNext(ThirdAppLoginRes response) {
                super.onNext(response);
                ((IGetCodeContact.IGetCodeView) mView).thirdAppLoginFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IGetCodeContact.IGetCodeView) mView).thirdAppLoginFail(throwable.getMessage());

            }
        });
    }

    @Override
    public void checkThirdApp(String userPhone) {
        CheckThirdAppReq checkThirdAppReq = new CheckThirdAppReq();
        checkThirdAppReq.setDevice_id(AndroidUtils.getAndroidId(DaggerApplication.getIns()));
        String login_type = ACache.get(DaggerApplication.getIns()).getAsString("login_type");
        checkThirdAppReq.setLogin_type(login_type);
        checkThirdAppReq.setMobile(userPhone);
        String json = new Gson().toJson(checkThirdAppReq);
        L.e(Constant.TAG, "checkThirdAppReq---->" + json);
        mNetworkDateSource.checkThirdAppMobile(checkThirdAppReq, new SubscribeResult<CheckThirdAppRes>() {
            @Override
            protected void onOk(CheckThirdAppRes response) {
                String toJson = new Gson().toJson(response);
                L.e(Constant.TAG, "checkThirdAppMobile---->" + toJson);
                if (response != null) {
                    ((IGetCodeContact.IGetCodeView) mView).checkThirdAppSucc(response);
                }
            }

            @Override
            protected void onFailure(CheckThirdAppRes response) {
                super.onFailure(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void serviceAgreement() {

    }

    //根据场景获取协议
    @Override
    public void getProtocols(String sceneNo) {
        ProtocolsRequest protocolsRequest = new ProtocolsRequest();
        protocolsRequest.setSceneNo(sceneNo);

        mNetworkDateSource.getProtocols(protocolsRequest, new SubscribeResult<ProtocolsRes>() {

            @Override
            protected void onOk(ProtocolsRes response) {
                ((IGetCodeContact.IGetCodeView) mView).getProtocolsSucc(response);
            }

            @Override
            protected void onFailure(ProtocolsRes res) {
                ((IGetCodeContact.IGetCodeView) mView).getProtocolsFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IGetCodeContact.IGetCodeView) mView).getProtocolsFail();
            }
        });
    }

    @Override
    public void getImgCode(String phone) {
        ImgCodeRequest imgCodeRequest = new ImgCodeRequest();
        imgCodeRequest.setMobile(phone);

        mNetworkDateSource.getImgCode(imgCodeRequest, new SubscribeResult<ImgCodeRes>() {

            @Override
            protected void onOk(ImgCodeRes response) {
                ((IGetCodeContact.IGetCodeView) mView).getImgCodeSucc(response);
            }

            @Override
            protected void onFailure(ImgCodeRes res) {
                ((IGetCodeContact.IGetCodeView) mView).getImgCodeFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IGetCodeContact.IGetCodeView) mView).getImgCodeFail();
            }
        });
    }
}
